<?php

namespace app\front\controller;

use think\Controller;
use think\Loader;
use think\Request;
use think\Session;

class User extends Controller
{
    //登录页面
    public function login()
    {
        return view();
    }

//    执行登录
    public function doLogin()
    {
        $data = input();
//       验证

        $validate = Loader::validate('User');
        if (!$validate->scene('login')->check($data)) {
            $this->error($validate->getError());
        }
        //设置token
        $token = \Token::create_token('token');
        Session::set('token', $token);
        $data['token'] = $token;
        $res = \app\front\model\User::login($data);
        if (!$res) {
            $this->error('账号或密码有误...', '/front/user/login');
        }else{
//            设置
            Session::set('id',$res['id']);
            return json(['code' => 200, 'msg' => '登录成功', 'data' => $data]);
        }

    }
//    销毁登录
    public function nosession(){
        session(null, 'think');
    }
}
